package com.project.music.controller;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.project.music.bean.Ranks;
import com.project.music.service.RanksService;
import com.project.music.utils.Consts;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 评分控制类
 */

@Api(tags = "评分控制类")
@RestController
@RequestMapping("/rank")
public class RankController {

    @Autowired
    private RanksService ranksService;

    //提交评分
    @ApiOperation("提交评分")
    @RequestMapping("add")
    public JSONObject addRank(Ranks ranks) {
        JSONObject jsonObject = new JSONObject();

        QueryWrapper<Ranks> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda()
                    .eq(Ranks::getConsumerId, ranks.getConsumerId())
                    .eq(Ranks::getSongListId, ranks.getSongListId());
        boolean status = ranksService.saveOrUpdate(ranks, queryWrapper);
        if (status) {
            jsonObject.put(Consts.CODE, 1);
            jsonObject.put(Consts.MSG, "提交成功");
        } else {
            jsonObject.put(Consts.CODE, 0);
            jsonObject.put(Consts.MSG, "提交失败");
        }
        return jsonObject;
    }
    //获取指定歌单的评分
    @ApiOperation("获取指定歌单的评分")
    @RequestMapping("")
    public Integer rankOfSongList(@RequestParam("songListId") @ApiParam("歌单ID") int songListId) {
        QueryWrapper<Ranks> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda()
                    .eq(Ranks::getSongListId, songListId);
        List<Ranks> rankList = ranksService.list(queryWrapper);
        int scoreSum = 0;
        for (Ranks ranks: rankList) {
            scoreSum += ranks.getScore();
        }
        return scoreSum/rankList.size();
    }
}
